import React, {Component} from 'react';

import './index.less';

import HRPagination from 'src/hrpub/common/components/Pagination';

class MainTable extends Component {
    constructor(props) {
        super(props);

        this.changePage = this.changePage.bind(this);
        this.changePageSize = this.changePageSize.bind(this);
        this.clickHandle = this.clickHandle.bind(this);
    }

    changePageSize(value) {
        this.changePagination({
            pageSize: value,
            pageIndex: 1,
            mainTableHeight: 30 * value
        });
    }

    changePagination(nPageInfo) {
        const {
            pageInfo,
            updateState,
            getMainTableData
        } = this.props;

        updateState({
            pageInfo: {
                ...pageInfo,
                ...nPageInfo
            },
            // 是否允许跨页面选择
            selectedRows: []
        }, () => {
            getMainTableData();
        });
    }

    changePage(value) {
        this.changePagination({
            pageIndex: value
        });
    }

    clickHandle(props, moduleId, record, index) {
        const {
            subsetVisible,
            updateState,
            insRef
        } = this.props;
        updateState({
            clickRow: {
                index: index,
                record: record
            }
        });
        setTimeout(() => {
            subsetVisible && insRef.subSetCard.initSubSet();
        }, 1);
    }

    onSelected = (props, moduleId, record, index, isSelected) => {
        const {subsetVisible, insRef} = this.props;
        this.props.editTable.setClickRowIndex('ncc60071701list', {
            record: record,
            index: index
        });
        this.props.editTable.focusRowByIndex('ncc60071701list', index);
        this.props.updateState({
            clickRow: {
                index: index,
                record: record
            }
        });
        setTimeout(() => {
            subsetVisible && insRef.subSetCard.initSubSet();
        }, 1);
        this.props.handleTableSelect(props, moduleId, record, index)
    };

    render() {
        const {
            editTable,
            pageInfo,
            showTableCheckbox,
            mainTableHeight,
            page,
            pageHeight,
            subsetVisible,
            subHeight
        } = this.props;

        let subH = subHeight ? subHeight : pageHeight * 0.3;
        return (
            <div>
                <div
                    className="info-table-wrapper"
                    style={{
                        // height: !subsetVisible ? `${pageHeight - 102}px` : `${pageHeight * 0.7 - 102}px`
                        height: !subsetVisible ? `${pageHeight - 102}px` : `${pageHeight - 102 - subH}px`
                    }}
                    id="mainTable"
                >
                    {editTable.createEditTable('ncc60071701list', {
                        showIndex: true,
                        showCheck: showTableCheckbox,
                        onSelected: this.onSelected,
                        onSelectedAll: this.props.selectAll,
                        onRowDoubleClick: (record) => {
                            if (page !== 'adjust-sort') {
                                this.props.updateState({
                                    selectedRows: [{data: record}],
                                    pageFrom: 'dbClick'
                                }, () => {
                                    this.props.toEditPage('dbClick');
                                });
                            }
                        },
                        onRowClick: this.clickHandle,
                        height: !subsetVisible ? pageHeight - 130 : pageHeight - subH - 130,
                        adaptionHeight: false,
                        autoAffixScroll: false
                    })}
                </div>
                <div
                    style={{
                        padding: '12px 20px'
                    }}
                >
                    {
                        pageInfo.total > 0 &&
                        <HRPagination
                            current={parseInt(pageInfo.pageIndex)}
                            pageSize={parseInt(pageInfo.pageSize)}
                            pageSizeOptions={[5, 10, 20, 50, 100]}
                            showSizeChanger={false}
                            total={pageInfo.total}
                            onShowSizeChange={this.changePageSize}
                            onChange={this.changePage}
                            showSizeInput={true}
                        />
                    }
                </div>

            </div>
        );
    }
}

export default MainTable;
